package top.zuli.view;

import top.zuli.model.Customer;
import top.zuli.service.CustomerList;

import java.util.Scanner;

public class CustomerView {
    //1.初始化容器
    CustomerList customerList = new CustomerList(10);
    //2.定义扫描类
    Scanner scanner = new Scanner(System.in);
    //3.编写主界面方法
    private void mainMenu() {
        while(true) {
            //1.界面显示
            System.out.println("\r\n--------------------客户管理系统--------------------");
            System.out.println("                    1.添加客户");
            System.out.println("                    2.修改客户");
            System.out.println("                    3.删除客户");
            System.out.println("                    4.客户列表");
            System.out.println("                    5.退出");
            System.out.println("请选择【1-5】");
            //2.获得客户的输入信息
            int number = scanner.nextInt();
            if(!(number==1||number==2||number==3||number==4||number==5)){
                System.out.println("输入有误，请重新输入");
                continue;
            }
            a:switch(number) {
                case 1:
                    //System.out.println("添加客户");
                    addCustomer();
                    break;
                case 2:
                    //System.out.println("修改客户");
                    modifyCustomer();
                    break;
                case 3:
                    //System.out.println("删除客户");
                    deleteCustomer();
                    break;
                case 4:
                    //System.out.println("客户列表");
                    viewCustomerList();
                    break;
                case 5:
                    while(true) {
                        System.out.println("确认是否退出y/n");
                        String str = scanner.next();
                        //忽略大小写比较内容
                        if(str.equalsIgnoreCase("y")) {
                            return;
                        }else if(str.equals("n")) {
                            break a;
                        }
                    }


            }
        }
    }
    //4.客户列表展示
    private void viewCustomerList() {
        System.out.println("--------------------客户列表--------------------");
        Customer[] customersAll = customerList.getCustomerList();
        if(customersAll.length==0) {
            System.out.println("没有客户记录");
        } else {
            System.out.println("编号\t姓名\t性别\t年龄\t手机号\t\t邮箱\t");
            for(int i=0;i<customersAll.length;i++) {
                System.out.println((i+1)+"\t"+customersAll[i].getName()+"\t"+customersAll[i].getSex()+"\t"+customersAll[i].getAge()+"\t"+customersAll[i].getPhone()+"\t"+customersAll[i].getEmail());
            }
        }
    }
    //5.客户添加功能
    private void addCustomer() {
        System.out.println("--------------------添加客户--------------------");
        System.out.println("姓名");
        String name = scanner.next();
        System.out.println("性别");
        String sex = scanner.next();
        System.out.println("年龄");
        int age = scanner.nextInt();
        System.out.println("手机号");
        String phone = scanner.next();
        System.out.println("邮箱");
        String email = scanner.next();
        Customer customer = new Customer(name, sex, age, phone, email);
        boolean tag = customerList.add(customer);
        if(tag) {
            System.out.println("添加成功");
        } else {
            System.out.println("记录已满，添加失败");
        }
    }
    //6.修改客户
    private void modifyCustomer() {
        System.out.println("--------------------修改客户--------------------");
        //1.下标
        int index = 0;
        //2.客户
        Customer customer;
        while(true) {
            System.out.println("请选择要修改的客户编号【-1退出】");
            //1.获得客户输入要修改的下标
            index = scanner.nextInt();
            if(index==-1) {
                return;
            } else {
                //获得要修改的客户
                customer = customerList.getCustomer(index-1);
                if(customer==null) {
                    System.out.println("无法找到要修改的客户");
                } else {
                    break;
                }
            }
        }
        //3.修改客户
        System.out.println("姓名("+customer.getName()+"):");
        String name = scanner.next();
        System.out.println("性别("+customer.getSex()+"):");
        String sex = scanner.next();
        System.out.println("年龄("+customer.getAge()+"):");
        int age = scanner.nextInt();
        System.out.println("手机号("+customer.getPhone()+"):");
        String phone = scanner.next();
        System.out.println("邮箱("+customer.getEmail()+"):");
        String email = scanner.next();
        customer = new Customer(name, sex, age, phone, email);
        boolean tag = customerList.modify(index-1, customer);
        if(tag) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }
    //7.删除客户
    private void deleteCustomer() {
        System.out.println("--------------------删除客户--------------------");
        //1.下标
        int index = 0;
        //2.客户
        Customer customer;
        while(true) {
            System.out.println("请选择要修改的客户编号【-1退出】");
            //1.获得客户输入要修改的下标
            index = scanner.nextInt();
            if(index==-1) {
                return;
            } else {
                //获得要删除的客户
                customer = customerList.getCustomer(index-1);
                if(customer==null) {
                    System.out.println("无法找到要删除的客户");
                } else {
                    break;
                }
            }
        }
        while(true) {
            System.out.println("确认删除y/n");
            String str = scanner.next();
            if(str.equalsIgnoreCase("y")) {
                break;
            }else if(str.equalsIgnoreCase("n")) {
                return;
            }

        }
        boolean tag = customerList.delete(index-1);
        if(tag) {
            System.out.println("删除成功");

        }else {
            System.out.println("删除失败");
        }
    }
    public static void main(String[] args) {
        //1.创建客户显示对象
        CustomerView customerView = new CustomerView();
        //2.调用目录方法
        customerView.mainMenu();
    }
}

